package com.example.shiwuyouproject.ui.fragment.my.activity
import android.content.Intent
import android.os.Bundle
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityPersonalinformationBinding
import com.example.shiwuyouproject.net.http.Api
import com.example.shiwuyouproject.net.http.QiNiuUpImageUtils
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.GetUserShareDataBean
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.PersonalInformationViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.ui.pup.AlbumPopupView
import com.example.shiwuyouproject.utils.ActStartUtils
import com.example.shiwuyouproject.utils.Auth
import com.example.shiwuyouproject.utils.GlideEngine
import com.example.shiwuyouproject.utils.GlideUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.lxj.xpopup.XPopup
import com.qiniu.android.common.FixedZone
import com.qiniu.android.storage.Configuration
import com.qiniu.android.storage.UploadManager
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import java.text.SimpleDateFormat
import java.util.*


/**
 * 个人信息
 */
class PersonalInformationActivity:BaseVmActivity<ActivityPersonalinformationBinding, PersonalInformationViewModel>(){
    override fun viewModelClass(): Class<PersonalInformationViewModel> =PersonalInformationViewModel::class.java
    override fun getViewBinding(): ActivityPersonalinformationBinding {
        return  ActivityPersonalinformationBinding.inflate(layoutInflater)
    }

    override fun initView() {
    mBinding.MyTitleBar.tvTitle.text="个人信息"

    }

    override fun initData() {
    mViewModel.getRealNameAuthenticationData()

    }
    var stNickName=""
    var stRealname=""
    override fun setListener() {
        mBinding.MyTitleBar.tvBack.setOnClickListener {finish()}
        mBinding.itemHeader.setOnClickListener { //个人头像修改
             QiNiuUpImageUtils.openCard(this)
        }
        mBinding.itemNickName.setOnClickListener {
            val mBundle=Bundle()
            mBundle.putString("titleType","nickName")
            if(stNickName.isNotEmpty()){
                mBundle.putString("stNickName",stNickName)
            }
            ActStartUtils.startForAct(this, ModifyRemarksActivity::class.java,mBundle,100)
        }
        mBinding.itemName.setOnClickListener {
            val mBundle=Bundle()
            mBundle.putString("titleType","name")
            if(stRealname.isNotEmpty()){
                mBundle.putString("stRealname",stNickName)
            }
            ActStartUtils.startForAct(this, ModifyRemarksActivity::class.java,mBundle,100)
        }
    }
    private var myKey=""
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode==PictureConfig.REQUEST_CAMERA) {
            YUtils.showLoading(this, "上传中...")
            QiNiuUpImageUtils.postUpQiNiuFile(data,object : QiNiuUpImageUtils.onBackImageStatus {
                override fun onSuccess(key: String) {
                    myKey =key
                    mViewModel.preservationCard(myKey)
                }
                override fun onFail() {
                    YUtils.hideLoading()
                    ToastUtil.show("图片上传失败，请检查网络连接")
                }
            })
        }else{
            if(resultCode==100&&requestCode==100){
                mBinding.tvnickname.text = data?.getStringExtra("titleType")
            }else    if(resultCode==100&&requestCode==101){
                mBinding.tvrealname.text = data?.getStringExtra("titleType")
            }
        }
    }
    override fun observe() {
        mViewModel.getPostStatus.observe(this,androidx.lifecycle.Observer<Boolean>{
            YUtils.hideLoading()
            if(it) {
                ToastUtil.show("上传成功！")
                GlideUtils.fangImgPortrait(this, mBinding.ivUploadIDCard, myKey )
            }else{
                ToastUtil.show("上传失败！")
            }
        })
        mViewModel.getGenRenStatus.observe(this,androidx.lifecycle.Observer<PersonalInformationBean>{
            myKey=it.avatar
            GlideUtils.fangImgPortrait(this, mBinding.ivUploadIDCard, myKey )
            mBinding.tvnickname.text=it.nickname
            mBinding.tvrealname.text=it.realname
            stNickName=it.nickname
            stRealname=it.realname

        })
    }
}